package org.example.Config;

import jakarta.annotation.PostConstruct;
import org.example.core.job.RunTaskJob;
import org.redisson.Redisson;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.starter.RedissonProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class RedissonConfig {


    @Bean
    RedissonClient redissonClient(RedisProperties redisProperties)
    {
        Config config = new Config();
        config.useSingleServer().setAddress(String.format("redis://%s:%s",redisProperties.getHost(),redisProperties.getPort()));


        return Redisson.create(config);
    }
    //由于配置Redisson会导致StringRedisTemplate失效，所以需要重新配置StringRedisTemplate
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisProperties redisProperties) {
        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());
        lettuceConnectionFactory.afterPropertiesSet();
        //lettuceConnectionFactory.start();
        return new StringRedisTemplate(lettuceConnectionFactory);
    }
}
